using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace OA.Mvc3.Controllers
{
    [ValidateInput(false)]
    public class ManagerGroupController :UI.BaseController
    {
        public const string ProgrameCode = "ManagerGroup";
        public static readonly string currentPath = UI.Utils.GetCurrentPath(ProgrameCode);

        public ManagerGroupController()
        {
            ViewBag.CurrentPath = currentPath;
        }

        [UI.ValidateAuthority(ProgrameCode)]
        public ActionResult Index()
        {
            return View();
        }

        [UI.ValidateAuthority(ProgrameCode)]
        public string List(string id)
        {
            string where = null;
            List<object> ps = new List<object>();
            if (!string.IsNullOrEmpty(id))
            {
                where = "ParentID=?";
                ps.Add(id);
            }
            else where = "ParentID is null";
            var list = new BLL.ManagerGroup().Get(where, null, 0, 0, ps.ToArray());
            List<object> listView = new List<object>();
            foreach (var i in list)
            {
                listView.Add(new
                {
                    id = i.ID,
                    text = Server.HtmlEncode(i.GroupName),
                    state = i.Childs.Count > 0 ? "closed" : null,
                    managerCount=i.Managers.Count
                });
            }
            return UI.Utils.ToJsonString(listView); 
        }

        [UI.ValidateAuthority(ProgrameCode , "delete")]
        public string Delete(string id)
        {
            var ret = new BLL.ManagerGroup().Delete(id);
            return UI.Utils.ToJsonString(ret);
        }

        [UI.ValidateAuthority(ProgrameCode , "add")]
        public ActionResult Add()
        {
            return View();
        }

        [HttpPost]
        [UI.ValidateAuthority(ProgrameCode , "add")]
        public string Add(Model.ManagerGroup model)
        {
            var ret=new BLL.ManagerGroup().Add(model);
            return UI.Utils.ToJsonString(ret);
            
        }

        [UI.ValidateAuthority(ProgrameCode , "edit")]
        public ActionResult Edit(string id)
        {
            var model = new BLL.ManagerGroup().Get(id);
            if (model == null) return UI.Utils.ErrorDateNoExist();
            return View(model);
        }

        [HttpPost]
        [UI.ValidateAuthority(ProgrameCode , "edit")]
        public string Edit(Model.ManagerGroup model)
        {
            var ret = new BLL.ManagerGroup().Update(model);
            return UI.Utils.ToJsonString(ret);
        }

        public string GroupTree(string id)
        {
            string where=null;
            List<object> ps = new List<object>();
            if (!string.IsNullOrEmpty(id))
            {
                where = "ParentID=?";
                ps.Add(id);
            }
            else where = "ParentID is null";
            var ret = new BLL.ManagerGroup().Get(where, null, 0, 0, ps.ToArray());
            List<object> trees = new List<object>();
            if (id == null)
            {
                trees.Add(new
                {
                    id = "",
                    text = "　",
                });
            }
            foreach (var i in ret)
            {
                trees.Add(new
                {
                    id = i.ID,
                    text = Server.HtmlEncode(i.GroupName),
                    state = i.Childs.Count > 0 ? "closed" : null
                });
            }
            return UI.Utils.ToJsonString(trees);
        }
    }
}
